from django.contrib import admin
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from gironimo.account.models import RegistrationProfile, UserProfile


class RegistrationAdmin(admin.ModelAdmin):
    actions = ['activate_users', 'resend_activation_email']
    list_display = ('user', 'activation_key_expired')
    raw_id_fields = ['user']
    search_fields = ('user__username', 'user__first_name', 'user__last_name')

    def activate_users(self, request, queryset):
        for profile in queryset:
            RegistrationProfile.objects.activate_user(profile.activation_key)
    activate_users.short_description = _('Benutzer aktivieren')

    def resend_activation_email(self, request, queryset):
        if Site._meta.installed:
            site = Site.objects.get_current()
        else:
            site = RequestSite(request)

        for profile in queryset:
            if not profile.activation_key_expired():
                profile.send_activation_email(site)
    resend_activation_email.short_description = _('Aktivierungsmails nochmals senden')


class UserProfileAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,                       {'fields': ['user',]}),
        (_('Benutzerinformationen'), {'fields': ['description', 'homepage', 'skype', 'city', 'avatar',]}),
        (_('HTML'),                  {'fields': ['html_title', 'html_description', 'html_keywords',], 'classes': ['collapse']}),
    ]
    list_display = ('user', 'created', 'homepage', 'city',)
    search_fields = ['user', 'city',]
    list_filter = ['city', 'created',]


admin.site.register(RegistrationProfile, RegistrationAdmin)
admin.site.register(UserProfile, UserProfileAdmin)

